package com.jojo.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jojo.domain.ProdComm;
import com.jojo.model.Result;
import com.jojo.service.ProdCommService;
import io.netty.util.internal.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * 商品评论管理控制层
 */
@Api("商品评论接口管理")
@RequestMapping("prod/prodComm")
@RestController
public class ProdCommController {
    @Autowired
    private ProdCommService prodCommService;

    @ApiOperation("多条件分页查询商品评论")
    @GetMapping("page")
    @PreAuthorize("hasAuthority('prod:prodComm:page')")
    public Result<Page<ProdComm>> loadProdCommPage(@RequestParam Long current,
                                                   @RequestParam Long size,
                                                   @RequestParam(required = false) String prodName,
                                                   @RequestParam(required = false) Integer status
                                                   ){
        Page<ProdComm> page = new Page<>(current,size);
        page = prodCommService.page(page,new LambdaQueryWrapper<ProdComm>()
                .like(StringUtils.hasText(prodName),ProdComm::getProdName,prodName)
                .eq(ObjectUtil.isNotNull(status),ProdComm::getStatus,status)
                .orderByDesc(ProdComm::getCreateTime)
        );
        return Result.success(page);
    }

    @ApiOperation("根据标识查询商品评论详情")
    @GetMapping("{prodCommId}")
    @PreAuthorize("hasAuthority('prod:prodComm:info')")
    public Result<ProdComm> loadProdCommInfo(@PathVariable Long prodCommId){
        ProdComm prodComm = prodCommService.getById(prodCommId);
        return Result.success(prodComm);
    }

    @ApiOperation("回复和审核商品评论")
    @PutMapping
    @PreAuthorize("hasAuthority('prod:prodComm:update')")
    public Result<?> replyAndExamineProdComm(@RequestBody ProdComm prodComm){
        Boolean updated = prodCommService.replyAndExamineProdComm(prodComm);
        return Result.handle(updated);
    }

}
